<?php
// +----------------------------------------------------------------------
// | 后台用户管理
// +----------------------------------------------------------------------
namespace app\admin\controller\v1;
use app\admin\Adminbase;
use app\common\model\UserLevel as UserLevelModel;
class UserLevel extends Adminbase
{
	
	/**
     * 获取等级
     * /admin/v1/user_level/xmlGetLevel
     * 需要登录
     * @is_need_login true
     * @method post
     */
    public function xmlGetLevel(){
        try {
            $model = new UserLevelModel();
            $model = $model-> field("name as label,cast(level as char) as value");
            $datas = $model-> select()->toArray();
          	array_unshift($datas,["label"=>"请选择等级","value"=>""]);
          	if( request()->isAjax() && request()->path() != 'admin/core/grid/getOption' ){
          		return $this->ReSucess($datas);
          	}else{
          		return $datas;
          	}
        } catch (\Exception $e) {
            return $this->ReError($e->getMessage());
        }
    }
	
    /**
     * 编辑
     * /admin/v1/user_level/edit
     * 需要登录
     * @is_need_login true
     * @method post
     */
    public function edit(){
        $request =  request();
        $param = $request ->post();
        $data_reward=json_decode($param['data'],true);
        try {
             //代理奖励制度表保存。
               $level_id = $param['level_id'];
                model('user_agent_reward')->where('user_level_id', $level_id)->delete();
                $result = model('user_agent_reward')
                    ->strict(false)
                    ->insertAll($data_reward);

            return $this->ReSucess();
        } catch (\Exception $e) {
            return $this->ReError($e->getMessage());
        }
    }

    
    /**
     * 增加
     * /admin/v1/user_level/save_agent_reward
     * 需要登录
     * @is_need_login true
     * @method post
     */
    public function save_agent_reward($data){
      
        $param = json_decode($data,true);
      
        try{
          
            $res = model("user_agent_reward")->allowField(true)->saveAll($param);
            return $this->ReSucess();   
        }catch(\Exception $e){
            return $this->ReError($e->getMessage());    
        }

    }

    //获取最大等级
    public function getMaxId(){
        try {
            return $this->ReSucess(model("user_agent_reward")->max('id') );   

        } catch (Exception $e) {
            return $this->ReError($e->getMessage());    
        }
    }
	
}

